home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / PROLOG / HUMBOLT / HUMBOLTS / _files / _humboltsr / SAVE._c < prev    next >
Text File  |  1990-06-10  |  11KB  |  445 lines

  1.  
  2. /***************************************************
  3. ****************************************************
  4. **                                                **
  5. **  HU-Prolog     Portable Interpreter System     **
  6. **                                                **
  7. **  Release 1.62   January  1990                  **
  8. **                                                **
  9. **  Authors:      C.Horn, M.Dziadzka, M.Horn      **
  10. **                                                **
  11. **  (C) 1989      Humboldt-University             **
  12. **                Department of Mathematics       **
  13. **                GDR 1086 Berlin, P.O.Box 1297   **
  14. **                                                **
  15. ****************************************************
  16. ***************************************************/
  17.  
  18. /*
  19. ** save(filename) generates a File which could be used
  20. ** instead of init.c in the synthesis of a new prolog system
  21. */
  22.  
  23. #include "systems.h"
  24. #include "atoms.h"
  25. #include "types.h"
  26. #include "files.h"
  27.  
  28. #if INITFILE
  29. #include <stdio.h>
  30.  
  31. IMPORT string RESTORESTATE;
  32. FILE *inifile;
  33.  
  34. void inierror(char *m)
  35. {
  36.     fprintf( stderr, "\nError in restore while: %s\n", m );
  37.     exit(1);
  38. }
  39.  
  40. #endif
  41.  
  42.  
  43. IMPORT TERM A0;
  44. IMPORT boolean DOTELL();
  45.  
  46. LOCAL int ENCODE_TERM(TERM T)
  47. {
  48.   IMPORT TERM LASTTERM;
  49.   if (T==nil_term || T==0) return 0;
  50. #if POINTEROFFSET
  51.   return ((int)LASTTERM-(int)T)/sizeof(TERMNODE)+1;
  52. #endif
  53. #if WORDOFFSET
  54.   return (int)LASTTERM-(int)T;
  55. #endif
  56. }
  57.  
  58. LOCAL TERM DECODE_TERM(int N)
  59. { IMPORT TERM LASTTERM;
  60.   if (N==0) return nil_term;
  61. #if POINTEROFFSET
  62.   return (TERM)((int)LASTTERM-(N-1)*sizeof(TERMNODE));
  63. #endif
  64. #if WORDOFFSET
  65.    return (TERM)((int)LASTTERM-(N-1));
  66. #endif
  67. }
  68.  
  69. /******************************/
  70. /*                            */
  71. /*  STRINGTAB                 */
  72. /*                            */
  73. /******************************/
  74.  
  75. IMPORT STRING BASESTRING,STRINGHTOP;
  76. IMPORT char st[];
  77. IMPORT int strhtop;
  78.  
  79. LOCAL void SAVE_STRINGTAB(void)
  80. { STRING I; int CH;
  81. #if INITFILE
  82.     ws( "STRINGHTOP " ); wi(STRINGHTOP); ws("\n");
  83.   for (I=BASESTRING;I<=STRINGHTOP;I++) 
  84.     { CH=repchar(I);  ws(" "); wi(CH);if (CH==0) ws("\n"); }
  85. #else
  86.   ws("\nchar st[]=\n{ ");
  87.   for (I=BASESTRING;I<STRINGHTOP;I++) 
  88.     { CH=repchar(I); wi(CH); ws(", "); if (CH==0) ws("\n  "); }
  89.   wi(repchar(STRINGHTOP));ws("\n};\n\n");
  90.   ws("int strhtop="); wi(STRINGHTOP); ws(";\n");
  91. #endif
  92. }
  93.  
  94. #if INITFILE
  95. int strhtop;
  96. #endif
  97.  
  98. LOCAL Init_Stringtab(void)
  99. { int I; STRING S;
  100. #if INITFILE
  101.   if( fscanf( inifile, "STRINGHTOP %d\n", &strhtop ) != 1 )
  102.     inierror( "Reading string header" );
  103.  
  104. #endif
  105.   STRINGHTOP= (STRING)strhtop;
  106.  
  107. #if INITFILE
  108.   for( S=BASESTRING; S<=STRINGHTOP; S++ ) {
  109.     if( fscanf( inifile, " %d", &I ) != 1 )
  110.       inierror( "Reading string" );
  111.     if( I==0 && fscanf( inifile, "\n" ) != 0 )
  112.       inierror( "Reading eoln string" );
  113.     repchar(S)=I;
  114.  
  115.   }
  116. #else
  117.   for(I=0,S=BASESTRING;S<=STRINGHTOP;I++,S++)
  118.     repchar(S)=st[I];
  119. #endif
  120. }
  121.  
  122.  
  123. /******************************/
  124. /*                            */
  125. /*  HASHTAB                   */
  126. /*                            */
  127. /******************************/
  128.  
  129. IMPORT ATOM HASHTAB[]; 
  130. IMPORT int HASH_SIZE;
  131. IMPORT ATOM ht[];
  132.  
  133. LOCAL void SAVE_HASHTAB(void)
  134.   int I;
  135. #if INITFILE
  136.   ws( "HASHTAB\n" );
  137.   for(I=0;I<=HASH_SIZE;I++ ) {
  138.      wi( HASHTAB[I] ); ws( "\n" );
  139.   }
  140. #else
  141.   ws("\nunsigned short ht[]=\n{ ");
  142.   for (I=0;I<HASH_SIZE;) 
  143.   { wi(HASHTAB[I++]); ws(","); if (I%16==0) ws("\n  "); }
  144.   wi(HASHTAB[HASH_SIZE]);
  145.   ws("\n};\n\n");
  146. #endif
  147. }
  148.  
  149. LOCAL Init_Hashtab(void)
  150. { int I;
  151. #if INITFILE
  152.   int hti;
  153.   if( fscanf( inifile, "HASHTAB\n" )  != 0)
  154.     inierror( "Reading hashtab header" );
  155.  
  156.   for( I =0 ; I <= HASH_SIZE; I++ ) {
  157.     if( fscanf( inifile, "%d\n", &hti ) != 1 )
  158.       inierror( "Reading hashtab" );
  159.     HASHTAB[I]=(ATOM)hti;
  160.   }
  161. #else
  162.   for(I=0;I<=HASH_SIZE;I++) HASHTAB[I]=(ATOM)ht[I];
  163. #endif
  164. }
  165.  
  166.  
  167. /******************************/
  168. /*                            */
  169. /*  ATOMTAB                   */
  170. /*                            */
  171. /******************************/
  172.  
  173. IMPORT ATOM BASEATOM,ATOMHTOP,LASTATOM,ATOMSTOP;
  174. IMPORT STRING STRINGSTOP;
  175. IMPORT struct { unsigned short ar,cl,ls,nx,ch,pr,in; } at[];
  176. IMPORT int athtop,lstatm;
  177.  
  178. LOCAL void save_atom(ATOM A)
  179. #if INITFILE
  180.   wi(arity(A)); ws(" "); 
  181.   if (A==MAIN_0) wi(0); else wi(ENCODE_TERM(clause(A))); ws(" "); 
  182.   wi(longstring(A)); ws(" ");
  183.   wi(nextatom(A)); ws(" ");
  184.   wi(chainatom(A)); ws(" ");
  185.   wi(oprec(A)); ws(" ");
  186.   wi(info(A)); ws( "\n" );
  187. #else
  188.   ws("{");
  189.   wi(arity(A)); ws(","); 
  190.   if (A==MAIN_0) wi(0); else wi(ENCODE_TERM(clause(A))); ws(","); 
  191.   wi(longstring(A)); ws(",");
  192.   wi(nextatom(A)); ws(",");
  193.   wi(chainatom(A)); ws(",");
  194.   wi(oprec(A)); ws(",");
  195.   wi(info(A));
  196.   ws("}");
  197. #endif
  198. }
  199.  
  200. LOCAL void SAVE_ATOMTAB(void)
  201. {
  202.   ATOM A;
  203. #if INITFILE
  204.   ws( "ATOMHTOP " ); wi( (int) ATOMHTOP ); ws( " " );
  205.   ws( "LASTATOM " ); wi( (int) LASTATOM ); ws( "\n" );
  206.   for (A=BASEATOM;A<=ATOMHTOP;inc_atom(A))
  207.      save_atom(A);
  208. #else
  209.   ws("struct { unsigned short ar,cl,ls,nx,ch,pr,in; } at[]=\n");
  210.   ws("{ ");
  211.   for (A=BASEATOM;A<ATOMHTOP;inc_atom(A)) { save_atom(A); ws(",\n  "); }
  212.   save_atom(ATOMHTOP); ws("\n");
  213.   ws("};\n\n");
  214.   ws("int athtop="); wi((int)ATOMHTOP); 
  215.   ws(",lstatm="); wi((int)LASTATOM); ws(";\n\n");
  216. #endif
  217. }
  218.        
  219. #if INITFILE
  220. int athtop, lstatm;
  221. #endif
  222.  
  223. LOCAL Init_Atomtab(void)
  224.   ATOM A; int I;
  225. #if INITFILE
  226.   int ar, cl, ls, nx, ch, pr, in;
  227.   if( fscanf( inifile, "ATOMHTOP %d LASTATOM %d\n", &athtop, &lstatm) != 2 )
  228.     inierror( "Reading Atoms header" );
  229. #endif
  230.   ATOMHTOP= (ATOM)athtop;
  231.   LASTATOM= (ATOM)lstatm;
  232.  
  233.   for (A=BASEATOM,I=0;A<=ATOMHTOP;inc_atom(A),I++)
  234.     {
  235. #if INITFILE
  236.       if( fscanf( inifile,
  237.                     "%d %d %d %d %d %d %d\n",
  238.                      &ar, &cl, &ls, &nx, &ch, &pr, &in )  != 7)
  239.         inierror( "Reading atoms" );
  240.       arity(A)=(ARITY_TYPE)ar;
  241.       clause(A)=DECODE_TERM(cl);
  242.       longstring(A)=(STRING)ls;
  243.       nextatom(A)=(ATOM)nx;
  244.       chainatom(A)=(ATOM)ch;
  245.       oprec(A)=(PREC_TYPE)pr;
  246.       info(A)=(INFO_TYPE)in;
  247. #else
  248.       arity(A)=(ARITY_TYPE)at[I].ar;
  249.       clause(A)=DECODE_TERM(at[I].cl);
  250.       longstring(A)=(STRING)at[I].ls;
  251.       nextatom(A)=(ATOM)at[I].nx;
  252.       chainatom(A)=(ATOM)at[I].ch;
  253.       oprec(A)=(PREC_TYPE)at[I].pr;
  254.       info(A)=(INFO_TYPE)at[I].in;
  255. #endif
  256. #if HACKY
  257.       nrofcalls(A)=0;
  258. #endif
  259.     }
  260.   nextatom(ATOMSTOP)=(card)STRINGSTOP;
  261. }
  262.  
  263.  
  264. /******************************/
  265. /*                            */
  266. /*  FREELIST                  */
  267. /*                            */
  268. /******************************/
  269.  
  270. IMPORT TERM freelist[];
  271. IMPORT int fl[];
  272.  
  273. LOCAL void SAVE_FREELIST(void)
  274. { int I; TERM T;
  275. #if INITFILE
  276.   ws( "FREELIST\n" );
  277.   for (I=0;I<=MAXARITY;I++) {
  278.     wi(ENCODE_TERM(freelist[I])); ws( "\n" );
  279.   }
  280. #else
  281.   ws("\nint fl[]=\n{ ");
  282.   for (I=0;I<MAXARITY;) 
  283.   { wi(ENCODE_TERM(freelist[I++])); ws(","); 
  284.     if (I%16==0) ws("\n  "); 
  285.   }
  286.   wi(ENCODE_TERM(freelist[MAXARITY])); 
  287.   ws("\n};\n\n");
  288. #endif
  289. }
  290.  
  291. LOCAL Init_Freelist(void)
  292. { int I; 
  293. #if INITFILE
  294.   int fl;
  295.   if( fscanf( inifile, "FREELIST\n" ) != 0)
  296.     inierror( "Reading Freelist header" );
  297.   for(I=0; I<= MAXARITY; I++ ) {
  298.     if( fscanf( inifile, "%d\n", &fl )  != 1)
  299.       inierror( "Reading Freelist" );
  300.     freelist[I]=DECODE_TERM(fl);
  301.   }
  302. #else   
  303.   for(I=0;I<=MAXARITY;I++) freelist[I]=DECODE_TERM(fl[I]);
  304. #endif
  305. }
  306.  
  307.  
  308. /******************************/
  309. /*                            */
  310. /*  TERMTAB                   */
  311. /*                            */
  312. /******************************/
  313.  
  314. IMPORT TERM HEAPTOP,LASTTERM;
  315. IMPORT CLAUSE IMPG;
  316. IMPORT unsigned short names[];
  317. IMPORT unsigned short sons[];
  318. IMPORT int hptop,ipg;
  319.  
  320. LOCAL void SAVE_TERMTAB(void)
  321. {
  322.   TERM T; int I;
  323. #if INITFILE
  324.   ws( "HEAPTOP " ); wi(ENCODE_TERM(HEAPTOP)); ws( "\n" );
  325.   ws( "IMPG " ); wi(ENCODE_TERM(IMPG)); ws("\n");
  326.   for (T=HEAPTOP;T<=LASTTERM;inc_term(T)) 
  327.     {
  328.       wi(name(T)); ws( " " ); 
  329.       if (name(T)==INTT) wi(ival(T));
  330.       else if (name(T)==SKELT) wi(offset(T));
  331.       else wi(ENCODE_TERM(son(T)));
  332.       ws( "\n" );
  333.     }
  334. #else
  335.   ws("unsigned short names[]=\n");
  336.   ws("{ ");
  337.   for (T=HEAPTOP,I=0;T<LASTTERM;inc_term(T)) 
  338.     { wi(name(T)); ws(", "); if (++I%8==0) ws("\n  ");  }
  339.   wi(name(LASTTERM)); ws("\n};\n\n");
  340.  
  341.   ws("unsigned short sons[]=\n");
  342.   ws("{ ");
  343.   for (T=HEAPTOP,I=0;T<LASTTERM;inc_term(T)) 
  344.     { if (name(T)==INTT) wi(ival(T));
  345.       else if (name(T)==SKELT) wi(offset(T));
  346.       else wi(ENCODE_TERM(son(T))); 
  347.       ws(", "); if (++I%8==0) ws("\n  ");  
  348.     } 
  349.   if (name(LASTTERM)==INTT) wi(ival(LASTTERM));
  350.   else if (name(LASTTERM)==SKELT) wi(offset(LASTTERM));
  351.   else wi(ENCODE_TERM(son(LASTTERM))); 
  352.   ws("\n};\n\n");
  353.  
  354.   ws("hptop="); wi(ENCODE_TERM(HEAPTOP)); 
  355.   ws(",ipg="); wi(ENCODE_TERM(IMPG)); ws(";\n");
  356. #endif
  357. }
  358.  
  359. #if INITFILE
  360. int hptop, ipg;
  361. #endif
  362.  
  363. LOCAL Init_Termtab(void)
  364.   TERM X; int I;
  365. #if INITFILE
  366.   int iname, ison;
  367.   if( fscanf( inifile, "HEAPTOP %d\n", &hptop ) != 1 )
  368.     inierror( "Reading Termtab header 1" );
  369.   if( fscanf( inifile, "IMPG %d\n", &ipg ) != 1)
  370.     inierror( "Reading Termtab header 2" );
  371. #endif
  372.   HEAPTOP = DECODE_TERM(hptop);
  373.   IMPG = DECODE_TERM(ipg);
  374.   for (X=HEAPTOP,I=0;X<=LASTTERM;inc_term(X),I++)
  375.     { 
  376. #if INITFILE
  377.       if( fscanf( inifile, "%d %d\n", &iname, &ison ) != 2)
  378.         inierror( "Reading Termtab" );
  379.       name(X)=(ATOM)iname;
  380.       if (name(X)==INTT) ival(X)=ison;
  381.       else if (name(X)==SKELT) offset(X)=ison;
  382.       else son(X)=DECODE_TERM(ison);
  383. #else
  384.       name(X)=(ATOM)names[I];
  385.       if (name(X)==INTT) ival(X)=sons[I];
  386.       else if (name(X)==SKELT) offset(X)=sons[I];
  387.       else son(X)=DECODE_TERM(sons[I]);
  388. #endif
  389.     }
  390. }
  391.  
  392.  
  393. /******************************/
  394. /*                            */
  395. /*  SAVE / INIT               */
  396. /*                            */
  397. /******************************/
  398.  
  399. GLOBAL boolean DOSAVE(void)
  400. {   
  401.     DOTELL();
  402. #if INITFILE
  403.     SAVE_TERMTAB();
  404.     SAVE_ATOMTAB();
  405.     SAVE_STRINGTAB();
  406.     SAVE_HASHTAB();
  407.     SAVE_FREELIST();
  408. #else
  409.     ws("\n\n\n");
  410.     SAVE_STRINGTAB();
  411.     SAVE_HASHTAB();
  412.     SAVE_ATOMTAB();
  413.     SAVE_FREELIST();
  414.     SAVE_TERMTAB();
  415.     ws("\n\n\n");
  416. #endif
  417.     CloseFile(outputfile);A0=mkatom(USER_0);
  418.     return DOTELL();
  419. }
  420.  
  421.  
  422. GLOBAL InitAll(void)
  423. {
  424. #if INITFILE
  425.   inifile = fopen( RESTORESTATE, "r" );
  426.   if( inifile == NULL )
  427.     inierror( "Opening saved state" );
  428. #endif
  429.   Init_Termtab();
  430.   Init_Atomtab();
  431.   Init_Stringtab();
  432.   Init_Hashtab();
  433.   Init_Freelist();
  434. #if INITFILE
  435.   if( fclose( inifile ) )
  436.     inierror( "Closing saved state" );
  437. #endif
  438. }
  439.  
  440.  
  441.